<?php
class NewsController extends AppController {

	var $name = 'News';
	var $uses = array('News');
	var $helpers = array('Time','Rss');
	var $components = array('Seo');
	
	function beforeFilter() {
		parent::beforeFilter();
	}

	function admin_index() {
		$this->News->recursive = 0;
		$this -> paginate = array('order' => 'News.created desc');
		$this->set('news', $this->paginate());
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this -> admin_seo();
			$this->News->create();
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('NEWS_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('NEWS_SAVED_ERROR',true));
			}
		}
		$users = $this->News->User->generateList();
		$this->set(compact('users'));
	}

	function admin_edit($id = null) {
		if (!$this -> News -> findById($id)) {
			$this->Session->setFlash(__('NEWS_INVALID',true));
			$this->redirect(array('action'=>'index'), null, true);
		}
		if (!empty($this->data)) {
			$this -> admin_seo();
			if ($this->News->save($this->data)) {
				$this->Session->setFlash(__('NEWS_SAVED_OK',true));
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash(__('NEWS_SAVED_ERROR',true));
			}
		} else {
			$this->data = $this->News->read(null, $id);
		}
		$users = $this->News->User->generateList();
		$this->set(compact('users'));
	}

	function admin_delete($id = null) {
		if (!$id || !$this -> News -> findById($id)) {
			$this->Session->setFlash(__('NEWS_INVALID',true));
		}
		if ($this->News->del($id)) {
			$this->Session->setFlash(__('NEWS_DELETED_OK',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}

	function admin_seo() {
		$this -> data['News']['seo_link'] = $this->Seo->create($this->data['News']['title']);
	}

	function admin_preview() {
		$preview = $this->data['News'];
		$this->set(compact("preview"));
	}
	
	function admin_delete_news() {
		if($this->News->deleteAll(array('News.id'=>$this->data['NewsDelete']))) {
			$this->Session->setFlash(__('NEWS_DELETED_OK',true));
		} else {
			$this->Session->setFlash(__('NEWS_DELETED_ERROR',true));
		}
		$this->redirect(array('action'=>'index'), null, true);
	}
	
	function index(){
		$this->News->recursive = 0;
		$this->paginate = array('limit'=>5,'order' => 'News.created desc');
		$this->set('news', $this->paginate());
	}
	
	function rss(){
		$this->set('news',$this->News->findAll(null,null,array('News.created'=>'desc')));
	}
	
	function view($newsSeoLink = null) {
		$created = '%'.$this->params['year'] . '-' . $this->params['month'] . '-' . $this->params['day'] . '%';
		$this->set("news", $this->News->find(array('News.seo_link' => $newsSeoLink,'News.created like "'.$created.'"')));
	}
}
?>